$().ready(function() {

var ap_CV = [3, 24,60];

// Add new td at title
var td = '<td><a href="http://www.astrowars.com/portal/Combat_Value"><small>CV</small></a></td>';
$('#awplus_content table:eq(0) tr[@bgcolor=#202060]:eq(0)').append(td);

// Fix colspan 5 to 6
$('#awplus_content table:eq(0) td[@colspan=5]').attr('colspan', 6);

$('#awplus_content table:first tr[@bgcolor!=#202060]:not(.ap_cv)').each(function() {
  var cv = 0;
  var f = $('td:gt(3):lt(7)', this).each(function(i) {
    cv += ap_CV[i] * parseInt($(this).text());
  });
  $(this).append("<td>"+cv+"</td>");
});

// CV totals

var ap_cv_totals = '<tr class="ap_cv" bgcolor="#202060"><td align="center" colspan="8"><strong>---- T o t a l s ----</strong></td></tr>';
ap_cv_totals += '<tr class="ap_cv" id="ap_cv_fl" bgcolor="#404040" align="center"><td>Fleets</td><td>--</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>--</td></tr>';
ap_cv_totals += '<tr class="ap_cv" id="ap_cv_cv" bgcolor="#404040" align="center"><td>Combat Values</td><td>--</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>';

$('#awplus_content table:eq(0) tr:last').after(ap_cv_totals);

// Total Fleets
$('#awplus_content table:first tr[@bgcolor!=#202060]:not(.ap_cv)').each(function() {
  $('td:gt(1):not(:last)', this).each(function(i) {
    var td = $('#ap_cv_fl td:eq('+(i+2)+')');
    var total = parseInt($(this).text()) + parseInt(td.text());
    td.text(total);
    delete td
  });
});

// Total CV
var tt = 0;
$('#ap_cv_fl td:gt(3):not(:last)').each(function(i) {
  var td = $('#ap_cv_cv td:eq('+(i+4)+')');
  var total = parseInt($(this).text()) * ap_CV[i];
  tt += total;
  td.text(total);
  delete td;
});
$('#ap_cv_cv td:last').html('<strong>'+tt+'</strong>');




$('#awplus_content a[@href^=/0/Map/]').each(function(i) {
  var id = this.href.match(/([0-9]+)$/)[1];
  this.id = id;
  this.href = "javascript:void(0);";

  $(this).click(function() {
    ap_showSystemDialog(id);
  });
});


});
